﻿using System.IO;
using System.Xml.Serialization;
using Com.Pernix.Common.File;

namespace Com.Pernix.Common.Xml
{
    public class XmlUtil
    {
        public static T Deserializer<T>(string xmlPath)
        {
            var deserializer = new XmlSerializer(typeof(T));
            using(TextReader textReader = new StreamReader(xmlPath))
            {
                var items = (T)deserializer.Deserialize(textReader);
                return items;
            }
        }

        public static void Serializer<T>(T data, string xmlPath)
        {
            var serializer = new XmlSerializer(typeof(T));
            using (var stream = System.IO.File.Open(xmlPath, FileMode.OpenOrCreate))
            {
                serializer.Serialize(stream, data);
            }
        }
        public static void Serializer<T>(T data, string xmlPath, bool createDirIfNotExist)
        {
            if (createDirIfNotExist)
            {
                FileUtil.CreateDirIfNotExist(xmlPath);
            }
            Serializer(data, xmlPath);
        }
        
    }
}
